<?php

/**
 * @author Sven-Ove Bjerkan
 * @copyright 2008
 */

class Destinations {
	/**
	 * Get all destinations
	 * @return an array with key 'the destinationID' and value with the destination name or -1 on error
	 */
	public function getDestinations() {
		$query = "SELECT * FROM destinations";
		
		$res = DB::singleton()->execSelectQuery($query);
		$dest = array();
		
		if ($res['numRows'] > 0) {
			for ($i = 0; $i < $res['numRows']; $i++) {
				$dest[$res['result'][$i]['destinationID']] = $res['result'][$i]['destinationName'];
			}
			return $dest;
		}
		else return -1;
	}
	
	/**
	 * Get form to add/update destination
	 * @param int $ID the id of the destination, if editing
	 * @param String $name the name of the destination, if editing
	 * @return the form
	 */
	public function showForm($ID, $name) {
		return '<form action="" method="POST">
				Stedsnavn: <input type="text" name="destName" value="'.$name.'">
				<input type="hidden" name="ID" value="'.$ID.'">
				<input type="submit" value="Lagre">
				</form>';	
	}
	
	/**
	 * Delete a destination and all flights and orderes related to it
	 * @param int $ID the destinationID
	 * @return boolean with status
	 */
	public function deleteDest($ID) {
		$ID = (int)$ID;
		if ($ID === 0) return false;
		
		// Get all flightIDs with this destination
		$query = "DELETE FROM flight WHERE destinationID=?";
		$query = DB::singleton()->getQuery($query, array('i'), array($ID));
		$res = DB::singleton()->execDeleteQuery($query);
		
		// Make sure all is deleted, so do it all in one commit
		DB::singleton()->setAutocommit(false);
		
		$query = array();
		
		// Found flights
		if ($res['numRows'] > 0) {
			for ($i = 0; $i < $res['numRows']; $i++) {
				$query[$i] = 'DELETE FROM order WHERE flightID=?';
				$query[$i] = DB::singleton()->getQuery($query[$i], array('i'), array($res['result'][$i]['flightID']));
			}
		}
		
		$query[] = "DELETE FROM destinations WHERE destinationID=?";
		$query[sizeof($query)-1] = DB::singleton()->getQuery($query[sizeof($query)-1], array('i'), array($ID));
		
		$query[] = "DELETE FROM flight WHERE destinationID=?";
		$query[sizeof($query)-1] = DB::singleton()->getQuery($query[sizeof($query)-1], array('i'), array($ID));
				
		for ($i = 0; $i < sizeof($query); $i++) {
			DB::singleton()->execDeleteQuery($query[$i]);
		}
		
		// Commit the queries
		$res = DB::singleton()->commit();
		
		// If it failed, do a rollback
		if (!$res) {
			DB::singleton()->rollback();	
		}
		
		// Re-enable autocommit
		DB::singleton()->setAutocommit(true);
		
		return $res;
	}
	
	/**
	 * Create a new destination
	 * @param String $destName the name of the new destination
	 * @return boolean with status
	 */
	public function createNewDest($destName) {
		$query = "INSERT INTO destinations (destinationName) VALUES (?)";
		$query = DB::singleton()->getQuery($query, array('s'), array($destName));
		
		return DB::singleton()->execInsertQuery($query);
	}
	
	/**
	 * Update a destination
	 * @param int $destID the destinationID
	 * @param String $destName the new destination name
	 * @return boolean with status
	 */
	public function updateDest($destID, $destName) {
		$query = "UPDATE destinations SET destinationName=? WHERE destinationID=?";
		$query = DB::singleton()->getQuery($query, array('s', 'i'), array($destName, $destID));
		
		return DB::singleton()->execUpdateQuery($query);
	}
}

?>